<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/25
 * Time: 16:25
 */
    namespace Admin\Controller;
    use Admin\Controller\AdminController;

    class GoodsController extends AdminController
    {
        //后台商品列表首页,加载
        public function index ()
        {
            $this->assign("title","商品列表");
            //要查询所有商品分类的id 和 name 根据 path 排名
            $goods = A("Goods","Event");
            $data = $goods->queryGoods();
            $goodslist = $data['list'];
            $show = $data['show'];
            //获得图片的路径
            //图片的路径
            foreach($goodslist as $key => $value){
                $ImgName = $this->get_img_url(__ROOT__."/Uploads/GoodsImg/", $value["img"], 50);
                $goodslist[$key]['img'] = $ImgName;
            }
            $this->assign("goodslist",$goodslist);
            $this->assign("page",$show);
            $this->display("Goods/index");
        }
        //后台商品添加页,加载
        public function add ()
        {
            $this->assign("title","商品添加");
            //要查询所有商品分类的id 和 name 根据 path 排名
            $cate = A("Goods","Event");
            $catelist = $cate -> queryAllCate();
            $this->assign("catelist",$catelist);
            $this->display("Goods/add");
        }
        //后台商品添加
        public function insert ()
        {
            if(IS_POST){
                $goods = A("Goods","Event");
                $res = $goods -> insert();
                if($res == "添加成功"){
                    $this->success("添加成功",U("Goods/index"));
                }else{
                    $this->error("添加失败");
                }
            }
        }

        //商品上架切换
        Public function change_show()
        {
            if(IS_POST){
                $goods = A("Goods","Event");
                $res = $goods -> change_show();
                $this->Ajaxreturn($res);
            }
        }
        //商品热销切换
        Public function change_hot()
        {
            if(IS_GET){
                $goods = A("Goods","Event");
                $res = $goods -> change_hot();
                echo $res;
            }
        }
        //商品新品切换
        Public function change_new()
        {
            if(IS_GET){
                $goods = A("Goods","Event");
                $res = $goods -> change_new();
                echo $res;
            }
        }

        //商品信息编辑,根据id查询
        public function edit()
        {
            if(IS_GET){
                $goods = A("Goods","Event");
                $res = $goods -> look();
                $this->assign("title","商品详情");
                $this->assign("goodsinfo",$res);
                //要查询所有商品分类的id 和 name 根据 path 排名
                $cate = A("Goods","Event");
                $catelist = $cate -> queryAllCate();
                $this->assign("catelist",$catelist);
                $this->display("Goods/detailPage");
            }
        }

        //商品信息编辑提交处理
        public function editDo ()
        {
            if(IS_POST){
                $goods = A("Goods","Event");
                $res = $goods ->editDo();
                if($res > 0){
                    $this->success("修改成功",U("Goods/index"));
                }else{
                    $this->error("修改失败");
                }
            }
        }

        //商品图片编辑,根据id查询
        public function editImg ()
        {
            if(IS_GET){
                $this->assign("title","商品图片编辑");
                $goods = A("Goods","Event");
                $goodsImgList = $goods -> editImg();
                $this->assign("goodsImgList",$goodsImgList);
                $this->assign("goods_id",$goodsImgList[0]['goods_id']);
                //得到商品名
                $res = $goods -> look();
                $this->assign("name",$res['name']);
                $this->display("Goods/image");
            }
        }

        //给商品添加图片
        public function addImg ()
        {
            if(IS_POST){
                $goods = A("Goods","Event");
                $res = $goods -> addImg();
                if($res == "添加成功"){
                    $this->success($res);
                }else{
                    $this->error($res);
                }
            }
        }

        //给商品设置封面图
        public function setMain ()
        {
            if(IS_GET){
                $goods = A("Goods","Event");
                $res = $goods -> setMain();
                if($res > 0){
                    $this->success("设置封面成功");
                }else{
                    $this->error("设置封面失败");
                }
            }
        }

        //前台页面的商品图片显示设置
        public function setShow ()
        {
            if(IS_GET){
                $goods = A("Goods","Event");
                $res = $goods -> setShow();
                if($res > 0){
                    $this->success("商品图片显示与否设置成功");
                }else{
                    $this->error("商品图片显示与否设置失败");
                }
            }
        }

        //商品图片删除
        public function delImg ()
        {
            if(IS_GET){
                $goods = A("Goods","Event");
                $res = $goods -> delImg();
                if($res > 0){
                    $this->success("商品图片删除成功");
                }else{
                    $this->error("商品图片删除失败");
                }
            }
        }
        public function evaluteall(){
            $this->assign('title','全部评价');

            $evaAll = M('evaluate')
                ->alias("e")
                ->field("e.msg,e.id,u.name uname,g.name gname,e.grade,e.create_time")
                ->join("shopping_user u on e.user_id=u.id")
                ->join("shopping_goods g on e.goods_id=g.id")
                ->select();
            $this->assign('evaAll',$evaAll);
//            dump($evaAll);die;
            $this->display('Goods/evaluateAll');
        }

    }













